# This is a sample Python script.

# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.

import cv2 as cv
import numpy as np
import point
from pylsd import lsd


def resize_img(image, height=1800):
    h, w = image.shape[:2]
    pro = height / h
    s_w = int(w * pro)
    s_h = int(height)
    size = (s_w, s_h)
    img = cv.resize(image, size)
    return img, s_h, s_w, pro


path = '/Users/wanggh/Desktop/a.jpeg'
# path = '/Users/wanggh/Desktop/original.jpeg'
# path = '/Users/wanggh/Desktop/b.jpeg'

default_pro = 0.76
src = cv.imread(path)
src, src_h, src_w, src_pro = resize_img(src)
# 灰度
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
# 高斯平滑
gray = cv.GaussianBlur(gray, (3, 3), 0)
# 边缘检测
gray = cv.Canny(gray, 50, 150, apertureSize=3, L2gradient=False)
lines = lsd(gray)
for i in len(lines.shape[0]):
    pt1 = (int(lines[i, 0]), int(lines[i, 1]))
    pt2 = (int(lines[i, 2]), int(lines[i, 3]))
    width = lines[i, 4]
    cv.line(src, pt1, pt2, (0, 0, 255), int(np.ceil(width / 2)))
lines = lsd.detect(gray)[0]
drawn_img = lsd.drawSegments(src, lines)
cv.imshow("test", src)
cv.waitKey(0)
cv.destroyAllWindows()
